import { login } from '@/api/user'

const state = {
  Token: localStorage.getItem('token69-house') || '',
  userInfo: JSON.parse(localStorage.getItem('userInfo69-house')) || {}
}

const mutations = {
  setToken(state, data) {
    localStorage.setItem('token69-house', data)
    state.Token = data
  },
  removeToken(state, data) {
    localStorage.removeItem('token69-house')
    localStorage.removeItem('userInfo69-house')
    state.Token = ''
    state.userInfo = ''
  },
  getUserInfo(state, data) {
    localStorage.setItem('userInfo69-house', JSON.stringify(data))
    state.userInfo = data
  }
}

const actions = {
  async login(store, data) {
    const res = await login(data)
    console.log(res)
    store.commit('setToken', res.data.jwt)
    store.commit('getUserInfo', res.data.user)
  },
  async logout(store, data) {
    store.commit('removeToken')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

